<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>git 基本操作命令 | Finen&#39;s Blog</title>
    <meta name="generator" content="VuePress 1.4.1">
    <link rel="icon" href="/logo.png">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/icons/apple-touch-icon-152x152.png">
    <link rel="mask-icon" href="/icons/safari-pinned-tab.svg" color="#3eaf7c">
    <meta name="description" content="Stay Hungry! Stay Foolish!">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.331bc503.css" as="style"><link rel="preload" href="/assets/js/app.8657dc36.js" as="script"><link rel="preload" href="/assets/js/3.655fda8f.js" as="script"><link rel="preload" href="/assets/js/58.1db8af1f.js" as="script"><link rel="preload" href="/assets/js/11.32858d9c.js" as="script"><link rel="prefetch" href="/assets/js/1.af5e15a9.js"><link rel="prefetch" href="/assets/js/10.3d57a2e0.js"><link rel="prefetch" href="/assets/js/12.cef9d203.js"><link rel="prefetch" href="/assets/js/13.02beb1f0.js"><link rel="prefetch" href="/assets/js/14.7efc03c9.js"><link rel="prefetch" href="/assets/js/15.6bbbab3f.js"><link rel="prefetch" href="/assets/js/16.f90edb71.js"><link rel="prefetch" href="/assets/js/17.0d4d6c13.js"><link rel="prefetch" href="/assets/js/18.76dd8979.js"><link rel="prefetch" href="/assets/js/19.7da82e6f.js"><link rel="prefetch" href="/assets/js/20.55ee1642.js"><link rel="prefetch" href="/assets/js/21.088a02d1.js"><link rel="prefetch" href="/assets/js/22.73d5a697.js"><link rel="prefetch" href="/assets/js/23.829e24c3.js"><link rel="prefetch" href="/assets/js/24.2f258433.js"><link rel="prefetch" href="/assets/js/25.0a52df22.js"><link rel="prefetch" href="/assets/js/26.1c66cc2f.js"><link rel="prefetch" href="/assets/js/27.aa781144.js"><link rel="prefetch" href="/assets/js/28.b2cf7947.js"><link rel="prefetch" href="/assets/js/29.b61b1598.js"><link rel="prefetch" href="/assets/js/30.ba0ae8b1.js"><link rel="prefetch" href="/assets/js/31.7637d043.js"><link rel="prefetch" href="/assets/js/32.e552a016.js"><link rel="prefetch" href="/assets/js/33.334376b0.js"><link rel="prefetch" href="/assets/js/34.08c95f99.js"><link rel="prefetch" href="/assets/js/35.76b6b95d.js"><link rel="prefetch" href="/assets/js/36.539b6d80.js"><link rel="prefetch" href="/assets/js/37.850783a5.js"><link rel="prefetch" href="/assets/js/38.06c33708.js"><link rel="prefetch" href="/assets/js/39.85fc61d5.js"><link rel="prefetch" href="/assets/js/4.12c21fd1.js"><link rel="prefetch" href="/assets/js/40.cdc93796.js"><link rel="prefetch" href="/assets/js/41.be79357e.js"><link rel="prefetch" href="/assets/js/42.e1ea76e5.js"><link rel="prefetch" href="/assets/js/43.06bf748b.js"><link rel="prefetch" href="/assets/js/44.ebd514b5.js"><link rel="prefetch" href="/assets/js/45.12908515.js"><link rel="prefetch" href="/assets/js/46.78fb0130.js"><link rel="prefetch" href="/assets/js/47.1a3b6cfd.js"><link rel="prefetch" href="/assets/js/48.ef04cdb5.js"><link rel="prefetch" href="/assets/js/49.763fd944.js"><link rel="prefetch" href="/assets/js/5.e50a6b6f.js"><link rel="prefetch" href="/assets/js/50.c8a7a24d.js"><link rel="prefetch" href="/assets/js/51.aaa9e6c7.js"><link rel="prefetch" href="/assets/js/52.7d3a85df.js"><link rel="prefetch" href="/assets/js/53.00784a7d.js"><link rel="prefetch" href="/assets/js/54.bd3845dd.js"><link rel="prefetch" href="/assets/js/55.10a67e4b.js"><link rel="prefetch" href="/assets/js/56.eb909b5a.js"><link rel="prefetch" href="/assets/js/57.4b821666.js"><link rel="prefetch" href="/assets/js/59.d3045373.js"><link rel="prefetch" href="/assets/js/6.f25d4964.js"><link rel="prefetch" href="/assets/js/60.e1ccc619.js"><link rel="prefetch" href="/assets/js/61.eca4de24.js"><link rel="prefetch" href="/assets/js/62.be7238a5.js"><link rel="prefetch" href="/assets/js/63.a6ace044.js"><link rel="prefetch" href="/assets/js/64.bbd88e61.js"><link rel="prefetch" href="/assets/js/65.1f6c1848.js"><link rel="prefetch" href="/assets/js/66.d22e96e8.js"><link rel="prefetch" href="/assets/js/67.80ad6427.js"><link rel="prefetch" href="/assets/js/68.130aa1f6.js"><link rel="prefetch" href="/assets/js/69.4b778299.js"><link rel="prefetch" href="/assets/js/7.9c54e368.js"><link rel="prefetch" href="/assets/js/70.edcac904.js"><link rel="prefetch" href="/assets/js/71.90aa842c.js"><link rel="prefetch" href="/assets/js/72.4cb8aeae.js"><link rel="prefetch" href="/assets/js/73.6f272f7b.js"><link rel="prefetch" href="/assets/js/74.d36a7a16.js"><link rel="prefetch" href="/assets/js/75.362400fa.js"><link rel="prefetch" href="/assets/js/76.140673a6.js"><link rel="prefetch" href="/assets/js/77.5f1336bf.js"><link rel="prefetch" href="/assets/js/78.6105e050.js"><link rel="prefetch" href="/assets/js/79.dd680c04.js"><link rel="prefetch" href="/assets/js/8.786d8198.js"><link rel="prefetch" href="/assets/js/80.ebfb944f.js"><link rel="prefetch" href="/assets/js/81.892d344c.js"><link rel="prefetch" href="/assets/js/9.b0b77105.js">
    <link rel="stylesheet" href="/assets/css/0.styles.331bc503.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Finen's Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/post/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/archives/" class="nav-link">
  归档
</a></div><div class="nav-item"><a href="/tags/" class="nav-link">
  标签云
</a></div><div class="nav-item"><a href="/friends/" class="nav-link">
  友人帐
</a></div> <a href="https://github.com/hirCodd" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/post/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/archives/" class="nav-link">
  归档
</a></div><div class="nav-item"><a href="/tags/" class="nav-link">
  标签云
</a></div><div class="nav-item"><a href="/friends/" class="nav-link">
  友人帐
</a></div> <a href="https://github.com/hirCodd" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <!----> </aside> <main class="page"> <div class="title-content"><div class="el-card box-cards is-never-shadow"><!----><div class="el-card__body"><h2>git 基本操作命令</h2> <div class="page-info"><i aria-hidden="true" class="el-icon-s-custom"></i> <span class="i-text">Finen</span> <i aria-hidden="true" class="el-icon-collection-tag"><span class="i-text">git</span></i> <i aria-hidden="true" class="el-icon-time"></i> <span class="i-text">2018-03-27</span> <span id="/blog/others/git/" data-flag-title="git 基本操作命令" class="leancloud-visitors"><i aria-hidden="true" class="el-icon-view"></i> <i class="leancloud-visitors-count">1000000</i></span></div></div></div></div> <div class="theme-default-content content__default"><h1 id="注册"><a href="#注册" class="header-anchor">#</a> 注册</h1> <p><a href="https://github.com/" target="_blank" rel="noopener noreferrer">git-register<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h1 id="git与svn的基本介绍"><a href="#git与svn的基本介绍" class="header-anchor">#</a> git与svn的基本介绍</h1> <h2 id="git与svn"><a href="#git与svn" class="header-anchor">#</a> git与svn</h2> <h3 id="svn"><a href="#svn" class="header-anchor">#</a> svn</h3> <p>SVN是集中式版本控制系统，版本库是集中放在中央服务器的，而干活的时候，用的都是自己的电脑，所以首先要从中央服务器哪里得到最新的版本，然后干活，干完后，需要把自己做完的活推送到中央服务器。一般公司用svn的比较多，至于原因,嘿嘿嘿....</p> <h3 id="git"><a href="#git" class="header-anchor">#</a> git</h3> <p>git是一个分布式版本控制系统，没有中心服务器，每个人的电脑就是一个版本库，你的电脑上的Gitrepository就是你的版本库，那多人之间是如何合作的呢？这样，</p> <blockquote><p>A在A的电脑上修改了A.Gitrepository里面的文件,而B在B的电脑上修改了clone过来的A.Gitrepository，你们两个只要把各自的修改推送给对方就行了，这就是为什么你就是没有网络也能使用版本控制。</p></blockquote> <h1 id="下载"><a href="#下载" class="header-anchor">#</a> 下载</h1> <h2 id="网址"><a href="#网址" class="header-anchor">#</a> 网址</h2> <p><a href="https://git-scm.com/downloads" target="_blank" rel="noopener noreferrer">git-download<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="选择你的os版本即可"><a href="#选择你的os版本即可" class="header-anchor">#</a> 选择你的OS版本即可</h2> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git.png" alt="git"></p> <h1 id="git基本命令"><a href="#git基本命令" class="header-anchor">#</a> git基本命令</h1> <p>0.你必须安装了git才能继续往下看。
1.新建一个目录(在你磁盘空闲较多的地方，新建一个目录(名称Repository))===&gt;这个Repository就是上面说的你的本地仓库，然后右键选择Git Bash(Git 命令行)，在目录下输入以下命令：</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code>git config <span class="token punctuation">-</span><span class="token punctuation">-</span>global user.name &quot;username&quot;

git config <span class="token punctuation">-</span><span class="token punctuation">-</span>global user.email &quot;youremail&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><blockquote><p>这里的<font color="red">username</font>就是你刚刚注册的用户名，<font color="red">email</font>就是刚刚注册邮箱。</p></blockquote> <h2 id="git-init"><a href="#git-init" class="header-anchor">#</a> git init</h2> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_init.png" alt="git_init"></p> <p>此时，你就创建了一个本地仓库了。可以在你的本地查看一下：</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_intit2.png" alt="git"></p> <h2 id="git-add-（把项目提交到git仓库）"><a href="#git-add-（把项目提交到git仓库）" class="header-anchor">#</a> git add （把项目提交到git仓库）</h2> <p>新建一个demo.txt的文件，并且在里面写入123456789</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_add.png" alt="git_add"></p> <p>这样就说明你已经把文件提交到文件暂存区了，接下来使用下一个命令你就可以在本地仓库看到了。</p> <h2 id="git-commit"><a href="#git-commit" class="header-anchor">#</a> git commit</h2> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_commit.png" alt="git_commit"></p> <blockquote><p>这里的<font color="red"> -m</font>就是你提交的注释，记得以后写一定要使用注释！这是一个好习惯！</p></blockquote> <h2 id="git-status-查看状态"><a href="#git-status-查看状态" class="header-anchor">#</a> git status ---查看状态</h2> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_status.png" alt="git_status"></p> <blockquote><p>没有什么东西需要提交，别急，我们在刚刚的demo.txt中<font color="red">追加一个0000</font>,记得保存文件。</p></blockquote> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_status_1.png" alt="git_status2"></p> <blockquote><p>修改之后就已经有了文件修改的信息，并且git提示你需要commit！所以你需要使用上面的git commit 提交！</p></blockquote> <h2 id="git-diff"><a href="#git-diff" class="header-anchor">#</a> git diff</h2> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_diff.png" alt="git_diff"></p> <blockquote><p>git diff就是比对文件，红色的是告诉你，那是原来的，绿色的就是告诉添加了信息。</p></blockquote> <blockquote><p>之后你就可以放心的提交你的版本了。</p></blockquote> <p><strong>你需要执行git add， git commit -m &quot;xxxx&quot;</strong>，必须通过add提交到文件暂存区。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_add_commit.png" alt="git_add_commit1"></p> <h1 id="版本控制-回退"><a href="#版本控制-回退" class="header-anchor">#</a> 版本控制---回退</h1> <h2 id="git-log"><a href="#git-log" class="header-anchor">#</a> git log</h2> <p>在使用git的时候一定要会使用日志功能。</p> <p>首先我们在demo.txt中添加一行数据<font color="red">0000123456789</font>,然后git add ，git commit.</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_add_commit2.png" alt="git_diff_add_commit"></p> <p>接下来使用git log打印日志</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_log.png" alt="git_log"></p> <h2 id="版本回滚-git-reset-hard-head"><a href="#版本回滚-git-reset-hard-head" class="header-anchor">#</a> 版本回滚---git reset --hard HEAD^</h2> <p>上面提到使用git log打印所有版本，其实有更简单的办法。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git log --pretty=oneline
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这个可以自己去尝试哟，这个只会提示你的<font color="red">版本号</font>跟<font color="red">提交的信息</font>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git reset --hard HEAD^
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_hard.png" alt="git_hard"></p> <blockquote><p>现在可以打开你的demo.txt看看里面还有什么内容了。是不是少了最后添加的一行数据，只有一行数据了。回滚到添加0000的版本了。</p></blockquote> <p><strong>超级回滚：</strong></p> <p>你需要回滚50个版本，总不能输入50个^号，所以有下面这种更简单的办法。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git reset --hard HEAD~50
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="cat命令"><a href="#cat命令" class="header-anchor">#</a> cat命令</h2> <p>使用cat查看内容。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/cat.png" alt="cat"></p> <p>然后你可以使用git log 查看一下存在的版本</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_log3.png" alt="git_log3"></p> <p>加入你要回滚到添加了<font color="red">0000123456789</font>的版本该怎么办呢？使用下一个命令+下下个命令！！！</p> <h2 id="git-reflog"><a href="#git-reflog" class="header-anchor">#</a> git reflog</h2> <blockquote><p>这个命令是告诉你所有操作的版本信息</p></blockquote> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_reflog.png" alt="git_reflog"></p> <p>看到版本号了---a0ca311，接下来就可以使用这个版本号进行回滚到添加<font color="red">0000123456789</font>的那个版本了。</p> <h2 id="git-reset-hard-版本号"><a href="#git-reset-hard-版本号" class="header-anchor">#</a> git reset --hard 版本号</h2> <p>下面是回滚到我的版本</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git reset --hard a0ca311
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_hard.png" alt="git_hard"></p> <p>接下来，打开你的demo.txt文档看一下是否已经有<font color="red">0000123456789</font>了。</p> <p>或者你可以使用以下命令</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>cat demo.txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><hr> <div class="language- extra-class"><pre><code> 下面会涉及删除与撤销的操作!请继续浏览！
</code></pre></div><hr></div> <footer class="page-edit"><!----> <!----></footer> <!---->  <div class="vcomment"><div id="vcomments"></div></div></main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.8657dc36.js" defer></script><script src="/assets/js/3.655fda8f.js" defer></script><script src="/assets/js/58.1db8af1f.js" defer></script><script src="/assets/js/11.32858d9c.js" defer></script>
  </body>
</html>
